Outlook macro runs through 250 iterations before failing with error [migrated]

Posted by Senoculus on Programmers See other posts from Programmers or by Senoculus
Published on 2012-11-09T16:11:34Z Indexed on 2012/11/09 17:19 UTC
Read the original article Hit count: 212

Filed under:
|
|

Description:

I have an Outlook macro that loops through selected emails in a folder and writes down some info to a .csv file. It works perfectly up until 250 before failing. Here is some of the code:

Open strSaveAsFilename For Append As #1

CountVar = 0
For Each objItem In Application.ActiveExplorer.Selection
    DoEvents
    If objItem.VotingResponse <> "" Then
        CountVar = CountVar + 1
        Debug.Print "   " & CountVar & ". " & objItem.SenderName
        Print #1,  & objItem.SenderName & "," &  objItem.VotingResponse
    Else
        CountVar = CountVar + 1
        Debug.Print "   " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to: Special Cases sub-folder"
        objItem.Move CurrentFolderVar.Folders("Special Cases")
    End If
Next
Close #1

Problem

After this code runs through 250 emails, the following screenshot pops up:

http://i.stack.imgur.com/yt9P8.jpg

I've tried adding a "wait" function to give the server a rest so that I'm not querying it so quickly, but I get the same error at the same point.

© Programmers or respective owner

Related posts about visual-basic

Related posts about macros